package backend.model.admin;

import backend.model.base.DBModel;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

import javax.persistence.*;
import java.util.Set;

@NamedQueries({ @NamedQuery(name = "findAllWorkplaces", query = "from Workplace"),
                @NamedQuery(name = "findAllActiveWorkplaces", query = "from Workplace where isActive = true"),
                @NamedQuery(name = "findWorkplaceByName", query = "from Workplace where name = :name")
              })
@Entity
@Table(name = "workplace")
public class Workplace extends DBModel {

	@Id
	@GeneratedValue
	@Column(name = "id")
	private int id;

	@Column(name = "name")
	private String name;

	@Column(name = "description")
	private String description;

    @Column(name = "is_deducted")
    private boolean isDeducted;

	@Column(name = "is_active")
	private boolean isActive;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "workplace")
    private Set<WorkDetail> workDetails;

	@Override
    public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

    public boolean getIsDeducted() {
        return isDeducted;
    }

    public void setIsDeducted(boolean isDeducted) {
        this.isDeducted = isDeducted;
    }

	public boolean getIsActive() {
		return isActive;
	}

	public void setIsActive(boolean isActive) {
		this.isActive = isActive;
	}

    public Set<WorkDetail> getWorkDetails() {
        return workDetails;
    }

    public void setWorkDetails(Set<WorkDetail> workDetails) {
        this.workDetails = workDetails;
    }

    @Override
    public String getDisplayLabel() {
		return name;
	}

	@Override
    public int compareTo(Object o) {
		if (!(o instanceof Workplace)) {
			return -1;
		} else {
			Workplace otherWorkplace = (Workplace) o;
			return getDisplayLabel().compareTo(otherWorkplace.getDisplayLabel());
		}
	}
}
